extends RigidBody2D

@export var move_speed: float = 1.0 
@export var rotate_speed: float = 30.0 
@export var move_range: float = 100.0  
@export var vertical_amplitude: float = 20.0 
@export var vertical_speed: float = 3 

var is_timed_out=false
var initial_position: Vector2
var direction: int 
var timer: Timer
var elapsed_time: float = 0.0 

func _ready():
	initial_position = position 
	gravity_scale = 0
	timer = $Timer
	timer.wait_time = 6 
	timer.start()  
	direction = randi_range(0, 1) * 2 - 1  # Randomly choose between 1 and -1

func _process(delta):
	# Slow down horizontal oscillation using a sine wave
	position.x = initial_position.x + move_range * sin(elapsed_time * move_speed)
	position.y = initial_position.y + vertical_amplitude * sin(elapsed_time * vertical_speed)
	elapsed_time += delta
	rotation_degrees += rotate_speed * delta
	if is_timed_out:
		$AudioStreamPlayer2D.play()
		await $AudioStreamPlayer2D.finished
		queue_free() 
		

# Called when the timer reaches its timeout
func _on_timer_timeout():
	is_timed_out=true



func _on_area_2d_area_entered(area):
	if not area.is_in_group('bubble'):
		$AudioStreamPlayer2D.play()
		await $AudioStreamPlayer2D.finished
		queue_free() 
